package fr.fermat.lyricsext;


import java.util.TimerTask;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.util.Log;


public class ConnectionTask extends TimerTask {

	private Activity activity;
	private ProgressDialog pdialog;
	private Tab lastTab;

	public ConnectionTask(Activity context, ProgressDialog pdialog, Tab lastTab) {
		super();
		this.activity = context;
		this.pdialog = pdialog;
		this.lastTab = lastTab;
	}

	@Override
	public void run() {
		Log.w("ConnectionTask", "Timer Task called");
		try {
		if (pdialog.isShowing()) {
			Log.w("ConnectionTask", "Timeout");
			activity.runOnUiThread(new Runnable() {

				public void run() {
					Log.w("ConnectionTask", "runOnUiThread called.");
					pdialog.dismiss();
					Log.w("ConnectionTask", "Dialog dismissed.");
					AlertDialog.Builder builder = new AlertDialog.Builder(
							activity);
					builder.setMessage(
							activity.getString(R.string.cancel_dialog))
							.setCancelable(true);
					AlertDialog aDialog = builder.create();
					aDialog.show();
					ActionBar actionbar = activity.getActionBar();
					if (lastTab.getPosition() != 0) {
						actionbar.selectTab(lastTab);
					} else {
						actionbar.selectTab(actionbar.getTabAt(0));
					}
				}
			});
		} else {
			Log.e("Dialog", "Dialog would schon dismissed");
		}
	} catch (NullPointerException np) {
	  Log.e("Dialog", "Dialog would schon dismissed");
   }	
  }
}
